[v4] Troubleshooting issue with Slim #2409
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What are you trying to accomplish?
Trying to upgrade ViewComponent to v4 in one of our apps, @xkraty and i noticed a lot of failures in CI like:
Couldn't find a template file or inline render method for SomeSlimComponent...
. I was like 🤔We use Slim everywhere and everything worked as expected with v3.
I tried to dig a bit and i think it could be a race condition between Slim initialization and the cache logic for each component.
I may be wrong so apologies in case all of this doesn't make sense!
In order:
In
ViewComponent::Base inherited
each component will be automatically compiled (and then cached) as soon as the file is loaded:view_component/lib/view_component/base.rb
Lines 544 to 547 in 6a33362
The problem comes with the Slim gem that will register the
slim
template handler only at theaction_view
hook causing a (possible) race condition within theViewComponent::Compiler.gather_templates
method:view_component/lib/view_component/compiler.rb
Lines 177 to 179 in 6a33362
ActionView::Template.template_handler_extensions
might not include "slim" at that point.All templates are gonna be memoized (with the related
template_errors
as well).If few components got loaded (and automatically cached due to
ViewComponent::Base.inherited
) before theaction_view
hook (that will register slim as a valid template handler)... those components will always raise theCouldn't find a template file or inline render method for ...
.What approach did you choose and why?
We created a Rails app trying to reproduce the issue with no luck. I'll try again tomorrow on it.
This PR is a basic/raw/dumb attempt to - at least - highlight the issue and give more context.
Anything you want to highlight for special attention from reviewers?
Nothing - just thanks for the great VC gem! 🙌